home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JDPIPE.C < prev    next >
C/C++ Source or Header  |  1991-12-01  |  49KB  |  1,311 lines

  1. /*
  2.  * jdpipe.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains decompression pipeline controllers.
  9.  * These routines are invoked via the d_pipeline_controller method.
  10.  *
  11.  * There are four basic pipeline controllers, one for each combination of:
  12.  *    single-scan JPEG file (single component or fully interleaved)
  13.  *  vs. multiple-scan JPEG file (noninterleaved or partially interleaved).
  14.  *
  15.  *    2-pass color quantization
  16.  *  vs. no color quantization or 1-pass quantization.
  17.  *
  18.  * Note that these conditions determine the needs for "big" images:
  19.  * multiple scans imply a big image for recombining the color components;
  20.  * 2-pass color quantization needs a big image for saving the data for pass 2.
  21.  *
  22.  * All but the simplest controller (single-scan, no 2-pass quantization) can be
  23.  * compiled out through configuration options, if you need to make a minimal
  24.  * implementation.  You should leave in multiple-scan support if at all
  25.  * possible, so that you can handle all legal JPEG files.
  26.  */
  27.  
  28. #include "jinclude.h"
  29.  
  30.  
  31. /*
  32.  * About the data structures:
  33.  *
  34.  * The processing chunk size for unsubsampling is referred to in this file as
  35.  * a "row group": a row group is defined as Vk (v_samp_factor) sample rows of
  36.  * any component while subsampled, or Vmax (max_v_samp_factor) unsubsampled
  37.  * rows.  In an interleaved scan each MCU row contains exactly DCTSIZE row
  38.  * groups of each component in the scan.  In a noninterleaved scan an MCU row
  39.  * is one row of blocks, which might not be an integral number of row groups;
  40.  * therefore, we read in Vk MCU rows to obtain the same amount of data as we'd
  41.  * have in an interleaved scan.
  42.  * To provide context for the unsubsampling step, we have to retain the last
  43.  * two row groups of the previous MCU row while reading in the next MCU row
  44.  * (or set of Vk MCU rows).  To do this without copying data about, we create
  45.  * a rather strange data structure.  Exactly DCTSIZE+2 row groups of samples
  46.  * are allocated, but we create two different sets of pointers to this array.
  47.  * The second set swaps the last two pairs of row groups.  By working
  48.  * alternately with the two sets of pointers, we can access the data in the
  49.  * desired order.
  50.  *
  51.  * Cross-block smoothing also needs context above and below the "current" row.
  52.  * Since this is an optional feature, I've implemented it in a way that is
  53.  * much simpler but requires more than the minimum amount of memory.  We
  54.  * simply allocate three extra MCU rows worth of coefficient blocks and use
  55.  * them to "read ahead" one MCU row in the file.  For a typical 1000-pixel-wide
  56.  * image with 2x2,1x1,1x1 sampling, each MCU row is about 50Kb; an 80x86
  57.  * machine may be unable to apply cross-block smoothing to wider images.
  58.  */
  59.  
  60.  
  61. /*
  62.  * These variables are logically local to the pipeline controller,
  63.  * but we make them static so that scan_big_image can use them
  64.  * without having to pass them through the quantization routines.
  65.  * If you don't support 2-pass quantization, you could make them locals.
  66.  */
  67.  
  68. static int rows_in_mem;        /* # of sample rows in full-size buffers */
  69. /* Full-size image array holding desubsampled, color-converted data. */
  70. static big_sarray_ptr *fullsize_cnvt_image;
  71. static JSAMPIMAGE fullsize_cnvt_ptrs; /* workspace for access_big_sarray() results */
  72. /* Work buffer for color quantization output (full size, only 1 component). */
  73. static JSAMPARRAY quantize_out;
  74.  
  75.  
  76. /*
  77.  * Utility routines: common code for pipeline controllers
  78.  */
  79.  
  80. LOCAL void
  81. interleaved_scan_setup (decompress_info_ptr cinfo)
  82. /* Compute all derived info for an interleaved (multi-component) scan */
  83. /* On entry, cinfo->comps_in_scan and cinfo->cur_comp_info[] are set up */
  84. {
  85.   short ci, mcublks;
  86.   jpeg_component_info *compptr;
  87.  
  88.   if (cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  89.     ERREXIT(cinfo->emethods, "Too many components for interleaved scan");
  90.  
  91.   cinfo->MCUs_per_row = (cinfo->image_width
  92.              + cinfo->max_h_samp_factor*DCTSIZE - 1)
  93.             / (cinfo->max_h_samp_factor*DCTSIZE);
  94.  
  95.   cinfo->MCU_rows_in_scan = (cinfo->image_height
  96.                  + cinfo->max_v_samp_factor*DCTSIZE - 1)
  97.                 / (cinfo->max_v_samp_factor*DCTSIZE);
  98.   
  99.   cinfo->blocks_in_MCU = 0;
  100.  
  101.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  102.     compptr = cinfo->cur_comp_info[ci];
  103.     /* for interleaved scan, sampling factors give # of blocks per component */
  104.     compptr->MCU_width = compptr->h_samp_factor;
  105.     compptr->MCU_height = compptr->v_samp_factor;
  106.     compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  107.     /* compute physical dimensions of component */
  108.     compptr->subsampled_width = jround_up(compptr->true_comp_width,
  109.                       (long) (compptr->MCU_width*DCTSIZE));
  110.     compptr->subsampled_height = jround_up(compptr->true_comp_height,
  111.                        (long) (compptr->MCU_height*DCTSIZE));
  112.     /* Sanity check */
  113.     if (compptr->subsampled_width !=
  114.     (cinfo->MCUs_per_row * (compptr->MCU_width*DCTSIZE)))
  115.       ERREXIT(cinfo->emethods, "I'm confused about the image width");
  116.     /* Prepare array describing MCU composition */
  117.     mcublks = compptr->MCU_blocks;
  118.     if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
  119.       ERREXIT(cinfo->emethods, "Sampling factors too large for interleaved scan");
  120.     while (mcublks-- > 0) {
  121.       cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  122.     }
  123.   }
  124.  
  125.   (*cinfo->methods->d_per_scan_method_selection) (cinfo);
  126. }
  127.  
  128.  
  129. LOCAL void
  130. noninterleaved_scan_setup (decompress_info_ptr cinfo)
  131. /* Compute all derived info for a noninterleaved (single-component) scan */
  132. /* On entry, cinfo->comps_in_scan = 1 and cinfo->cur_comp_info[0] is set up */
  133. {
  134.   jpeg_component_info *compptr = cinfo->cur_comp_info[0];
  135.  
  136.   /* for noninterleaved scan, always one block per MCU */
  137.   compptr->MCU_width = 1;
  138.   compptr->MCU_height = 1;
  139.   compptr->MCU_blocks = 1;
  140.   /* compute physical dimensions of component */
  141.   compptr->subsampled_width = jround_up(compptr->true_comp_width,
  142.                     (long) DCTSIZE);
  143.   compptr->subsampled_height = jround_up(compptr->true_comp_height,
  144.                      (long) DCTSIZE);
  145.  
  146.   cinfo->MCUs_per_row = compptr->subsampled_width / DCTSIZE;
  147.   cinfo->MCU_rows_in_scan = compptr->subsampled_height / DCTSIZE;
  148.  
  149.   /* Prepare array describing MCU composition */
  150.   cinfo->blocks_in_MCU = 1;
  151.   cinfo->MCU_membership[0] = 0;
  152.  
  153.   (*cinfo->methods->d_per_scan_method_selection) (cinfo);
  154. }
  155.  
  156.  
  157. LOCAL void
  158. reverse_DCT (decompress_info_ptr cinfo,
  159.          JBLOCKIMAGE coeff_data, JSAMPIMAGE output_data,
  160.          int start_row)
  161. /* Perform inverse DCT on each block in an MCU row's worth of data; */
  162. /* output the results into a sample array starting at row start_row. */
  163. /* NB: start_row can only be nonzero when dealing with a single-component */
  164. /* scan; otherwise we'd have to provide for different offsets for different */
  165. /* components, since the heights of interleaved MCU rows can vary. */
  166. {
  167.   DCTBLOCK block;
  168.   JBLOCKROW browptr;
  169.   JSAMPARRAY srowptr;
  170.   long blocksperrow, bi;
  171.   short numrows, ri;
  172.   short ci;
  173.  
  174.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  175.     /* calc size of an MCU row in this component */
  176.     blocksperrow = cinfo->cur_comp_info[ci]->subsampled_width / DCTSIZE;
  177.     numrows = cinfo->cur_comp_info[ci]->MCU_height;
  178.     /* iterate through all blocks in MCU row */
  179.     for (ri = 0; ri < numrows; ri++) {
  180.       browptr = coeff_data[ci][ri];
  181.       srowptr = output_data[ci] + (ri * DCTSIZE + start_row);
  182.       for (bi = 0; bi < blocksperrow; bi++) {
  183.     /* copy the data into a local DCTBLOCK.  This allows for change of
  184.      * representation (if DCTELEM != JCOEF).  On 80x86 machines it also
  185.      * brings the data back from FAR storage to NEAR storage.
  186.      */
  187.     { register JCOEFPTR elemptr = browptr[bi];
  188.       register DCTELEM *localblkptr = block;
  189.       register short elem = DCTSIZE2;
  190.  
  191.       while (--elem >= 0)
  192.         *localblkptr++ = (DCTELEM) *elemptr++;
  193.     }
  194.  
  195.     j_rev_dct(block);    /* perform inverse DCT */
  196.  
  197.     /* output the data into the sample array.
  198.      * Note change from signed to unsigned representation:
  199.      * DCT calculation works with values +-CENTERJSAMPLE,
  200.      * but sample arrays always hold 0..MAXJSAMPLE.
  201.      * Have to do explicit range-limiting because of quantization errors
  202.      * and so forth in the DCT/IDCT phase.
  203.      */
  204.     { register JSAMPROW elemptr;
  205.       register DCTELEM *localblkptr = block;
  206.       register short elemr, elemc;
  207.       register DCTELEM temp;
  208.  
  209.       for (elemr = 0; elemr < DCTSIZE; elemr++) {
  210.         elemptr = srowptr[elemr] + (bi * DCTSIZE);
  211.         for (elemc = 0; elemc < DCTSIZE; elemc++) {
  212.           temp = (*localblkptr++) + CENTERJSAMPLE;
  213.           if (temp < 0) temp = 0;
  214.           else if (temp > MAXJSAMPLE) temp = MAXJSAMPLE;
  215.           *elemptr++ = (JSAMPLE) temp;
  216.         }
  217.       }
  218.     }
  219.       }
  220.     }
  221.   }
  222. }
  223.  
  224.  
  225.  
  226. LOCAL JSAMPIMAGE
  227. alloc_sampimage (decompress_info_ptr cinfo,
  228.          int num_comps, long num_rows, long num_cols)
  229. /* Allocate an in-memory sample image (all components same size) */
  230. {
  231.   JSAMPIMAGE image;
  232.   int ci;
  233.  
  234.   image = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  235.                 (num_comps * SIZEOF(JSAMPARRAY));
  236.   for (ci = 0; ci < num_comps; ci++) {
  237.     image[ci] = (*cinfo->emethods->alloc_small_sarray) (num_cols, num_rows);
  238.   }
  239.   return image;
  240. }
  241.  
  242.  
  243. LOCAL void
  244. free_sampimage (decompress_info_ptr cinfo, JSAMPIMAGE image,
  245.         int num_comps, long num_rows)
  246. /* Release a sample image created by alloc_sampimage */
  247. {
  248.   int ci;
  249.  
  250.   for (ci = 0; ci < num_comps; ci++) {
  251.       (*cinfo->emethods->free_small_sarray) (image[ci], num_rows);
  252.   }
  253.   (*cinfo->emethods->free_small) ((void *) image);
  254. }
  255.  
  256.  
  257. LOCAL JBLOCKIMAGE
  258. alloc_MCU_row (decompress_info_ptr cinfo)
  259. /* Allocate one MCU row's worth of coefficient blocks */
  260. {
  261.   JBLOCKIMAGE image;
  262.   int ci;
  263.  
  264.   image = (JBLOCKIMAGE) (*cinfo->emethods->alloc_small)
  265.                 (cinfo->comps_in_scan * SIZEOF(JBLOCKARRAY));
  266.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  267.     image[ci] = (*cinfo->emethods->alloc_small_barray)
  268.             (cinfo->cur_comp_info[ci]->subsampled_width / DCTSIZE,
  269.              (long) cinfo->cur_comp_info[ci]->MCU_height);
  270.   }
  271.   return image;
  272. }
  273.  
  274.  
  275. LOCAL void
  276. free_MCU_row (decompress_info_ptr cinfo, JBLOCKIMAGE image)
  277. /* Release a coefficient block array created by alloc_MCU_row */
  278. {
  279.   int ci;
  280.  
  281.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  282.     (*cinfo->emethods->free_small_barray)
  283.         (image[ci], (long) cinfo->cur_comp_info[ci]->MCU_height);
  284.   }
  285.   (*cinfo->emethods->free_small) ((void *) image);
  286. }
  287.  
  288.  
  289. LOCAL void
  290. alloc_sampling_buffer (decompress_info_ptr cinfo, JSAMPIMAGE subsampled_data[2])
  291. /* Create a subsampled-data buffer having the desired structure */
  292. /* (see comments at head of file) */
  293. {
  294.   short ci, vs, i;
  295.  
  296.   /* Get top-level space for array pointers */
  297.   subsampled_data[0] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  298.                 (cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
  299.   subsampled_data[1] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  300.                 (cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
  301.  
  302.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  303.     vs = cinfo->cur_comp_info[ci]->v_samp_factor; /* row group height */
  304.     /* Allocate the real storage */
  305.     subsampled_data[0][ci] = (*cinfo->emethods->alloc_small_sarray)
  306.                 (cinfo->cur_comp_info[ci]->subsampled_width,
  307.                 (long) (vs * (DCTSIZE+2)));
  308.     /* Create space for the scrambled-order pointers */
  309.     subsampled_data[1][ci] = (JSAMPARRAY) (*cinfo->emethods->alloc_small)
  310.                 (vs * (DCTSIZE+2) * SIZEOF(JSAMPROW));
  311.     /* Duplicate the first DCTSIZE-2 row groups */
  312.     for (i = 0; i < vs * (DCTSIZE-2); i++) {
  313.       subsampled_data[1][ci][i] = subsampled_data[0][ci][i];
  314.     }
  315.     /* Copy the last four row groups in swapped order */
  316.     for (i = 0; i < vs * 2; i++) {
  317.       subsampled_data[1][ci][vs*DCTSIZE + i] = subsampled_data[0][ci][vs*(DCTSIZE-2) + i];
  318.       subsampled_data[1][ci][vs*(DCTSIZE-2) + i] = subsampled_data[0][ci][vs*DCTSIZE + i];
  319.     }
  320.   }
  321. }
  322.  
  323.  
  324. LOCAL void
  325. free_sampling_buffer (decompress_info_ptr cinfo, JSAMPIMAGE subsampled_data[2])
  326. /* Release a sampling buffer created by alloc_sampling_buffer */
  327. {
  328.   short ci, vs;
  329.  
  330.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  331.     vs = cinfo->cur_comp_info[ci]->v_samp_factor; /* row group height */
  332.     /* Free the real storage */
  333.     (*cinfo->emethods->free_small_sarray)
  334.         (subsampled_data[0][ci], (long) (vs * (DCTSIZE+2)));
  335.     /* Free the scrambled-order pointers */
  336.     (*cinfo->emethods->free_small) ((void *) subsampled_data[1][ci]);
  337.   }
  338.  
  339.   /* Free the top-level space */
  340.   (*cinfo->emethods->free_small) ((void *) subsampled_data[0]);
  341.   (*cinfo->emethods->free_small) ((void *) subsampled_data[1]);
  342. }
  343.  
  344.  
  345. LOCAL void
  346. duplicate_row (JSAMPARRAY image_data,
  347.            long num_cols, int source_row, int num_rows)
  348. /* Duplicate the source_row at source_row+1 .. source_row+num_rows */
  349. /* This happens only at the bottom of the image, */
  350. /* so it needn't be super-efficient */
  351. {
  352.   register int row;
  353.  
  354.   for (row = 1; row <= num_rows; row++) {
  355.     jcopy_sample_rows(image_data, source_row, image_data, source_row + row,
  356.               1, num_cols);
  357.   }
  358. }
  359.  
  360.  
  361. LOCAL void
  362. expand (decompress_info_ptr cinfo,
  363.     JSAMPIMAGE subsampled_data, JSAMPIMAGE fullsize_data,
  364.     long fullsize_width,
  365.     short above, short current, short below, short out)
  366. /* Do unsubsampling expansion of a single row group (of each component).  */
  367. /* above, current, below are indexes of row groups in subsampled_data;    */
  368. /* out is the index of the target row group in fullsize_data.             */
  369. /* Special case: above, below can be -1 to indicate top, bottom of image. */
  370. {
  371.   jpeg_component_info *compptr;
  372.   JSAMPARRAY above_ptr, below_ptr;
  373.   JSAMPROW dummy[MAX_SAMP_FACTOR]; /* for subsample expansion at top/bottom */
  374.   short ci, vs, i;
  375.  
  376.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  377.     compptr = cinfo->cur_comp_info[ci];
  378.     vs = compptr->v_samp_factor; /* row group height */
  379.  
  380.     if (above >= 0)
  381.       above_ptr = subsampled_data[ci] + above * vs;
  382.     else {
  383.       /* Top of image: make a dummy above-context with copies of 1st row */
  384.       /* We assume current=0 in this case */
  385.       for (i = 0; i < vs; i++)
  386.     dummy[i] = subsampled_data[ci][0];
  387.       above_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
  388.     }
  389.  
  390.     if (below >= 0)
  391.       below_ptr = subsampled_data[ci] + below * vs;
  392.     else {
  393.       /* Bot of image: make a dummy below-context with copies of last row */
  394.       for (i = 0; i < vs; i++)
  395.     dummy[i] = subsampled_data[ci][(current+1)*vs-1];
  396.       below_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
  397.     }
  398.  
  399.     (*cinfo->methods->unsubsample[ci])
  400.         (cinfo, (int) ci,
  401.          compptr->subsampled_width, (int) vs,
  402.          fullsize_width, (int) cinfo->max_v_samp_factor,
  403.          above_ptr,
  404.          subsampled_data[ci] + current * vs,
  405.          below_ptr,
  406.          fullsize_data[ci] + out * cinfo->max_v_samp_factor);
  407.   }
  408. }
  409.  
  410.  
  411. LOCAL void
  412. emit_1pass (decompress_info_ptr cinfo, int num_rows,
  413.         JSAMPIMAGE fullsize_data, JSAMPIMAGE color_data)
  414. /* Do color conversion and output of num_rows full-size rows. */
  415. /* This is not used for 2-pass color quantization. */
  416. {
  417.   (*cinfo->methods->color_convert) (cinfo, num_rows,
  418.                     fullsize_data, color_data);
  419.  
  420.   if (cinfo->quantize_colors) {
  421.     (*cinfo->methods->color_quantize) (cinfo, num_rows,
  422.                        color_data, quantize_out);
  423.  
  424.     (*cinfo->methods->put_pixel_rows) (cinfo, num_rows,
  425.                        &quantize_out);
  426.   } else {
  427.     (*cinfo->methods->put_pixel_rows) (cinfo, num_rows,
  428.                        color_data);
  429.   }
  430. }
  431.  
  432.  
  433. /*
  434.  * Support routines for 2-pass color quantization.
  435.  */
  436.  
  437. #ifdef QUANT_2PASS_SUPPORTED
  438.  
  439. LOCAL void
  440. emit_2pass (decompress_info_ptr cinfo, long top_row, int num_rows,
  441.         JSAMPIMAGE fullsize_data)
  442. /* Do color conversion and output data to the quantization buffer image. */
  443. /* This is used only with 2-pass color quantization. */
  444. {
  445.   short ci;
  446.  
  447.   /* Realign the big buffers */
  448.   for (ci = 0; ci < cinfo->num_components; ci++) {
  449.     fullsize_cnvt_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  450.       (fullsize_cnvt_image[ci], top_row, TRUE);
  451.   }
  452.  
  453.   /* Do colorspace conversion */
  454.   (*cinfo->methods->color_convert) (cinfo, num_rows,
  455.                     fullsize_data, fullsize_cnvt_ptrs);
  456.   /* Let quantizer get first-pass peek at the data. */
  457.   /* (Quantizer could change data if it wants to.)  */
  458.   (*cinfo->methods->color_quant_prescan) (cinfo, num_rows, fullsize_cnvt_ptrs);
  459. }
  460.  
  461.  
  462. METHODDEF void
  463. scan_big_image (decompress_info_ptr cinfo, quantize_method_ptr quantize_method)
  464. /* This is the "iterator" routine used by the quantizer. */
  465. {
  466.   long pixel_rows_output;
  467.   short ci;
  468.  
  469.   for (pixel_rows_output = 0; pixel_rows_output < cinfo->image_height;
  470.        pixel_rows_output += rows_in_mem) {
  471.     /* Realign the big buffers */
  472.     for (ci = 0; ci < cinfo->num_components; ci++) {
  473.       fullsize_cnvt_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  474.     (fullsize_cnvt_image[ci], pixel_rows_output, FALSE);
  475.     }
  476.     /* Let the quantizer have its way with the data.
  477.      * Note that quantize_out is simply workspace for the quantizer;
  478.      * when it's ready to output, it must call put_pixel_rows itself.
  479.      */
  480.     (*quantize_method) (cinfo,
  481.             (int) MIN(rows_in_mem,
  482.                   cinfo->image_height - pixel_rows_output),
  483.             fullsize_cnvt_ptrs, quantize_out);
  484.   }
  485. }
  486.  
  487. #endif /* QUANT_2PASS_SUPPORTED */
  488.  
  489.  
  490. /*
  491.  * Support routines for cross-block smoothing.
  492.  */
  493.  
  494. #ifdef BLOCK_SMOOTHING_SUPPORTED
  495.  
  496.  
  497. LOCAL void
  498. smooth_mcu_row (decompress_info_ptr cinfo,
  499.         JBLOCKIMAGE above, JBLOCKIMAGE input, JBLOCKIMAGE below,
  500.         JBLOCKIMAGE output)
  501. /* Apply cross-block smoothing to one MCU row's worth of coefficient blocks. */
  502. /* above,below are NULL if at top/bottom of image. */
  503. {
  504.   jpeg_component_info *compptr;
  505.   short ci, ri, last;
  506.   JBLOCKROW prev;
  507.  
  508.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  509.     compptr = cinfo->cur_comp_info[ci];
  510.     last = compptr->MCU_height - 1;
  511.  
  512.     if (above == NULL)
  513.       prev = NULL;
  514.     else
  515.       prev = above[ci][last];
  516.  
  517.     for (ri = 0; ri < last; ri++) {
  518.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  519.                 prev, input[ci][ri], input[ci][ri+1],
  520.                 output[ci][ri]);
  521.       prev = input[ci][ri];
  522.     }
  523.  
  524.     if (below == NULL)
  525.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  526.                 prev, input[ci][last], (JBLOCKROW) NULL,
  527.                 output[ci][last]);
  528.     else
  529.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  530.                 prev, input[ci][last], below[ci][0],
  531.                 output[ci][last]);
  532.   }
  533. }
  534.  
  535.  
  536. LOCAL void
  537. get_smoothed_row (decompress_info_ptr cinfo, JBLOCKIMAGE coeff_data,
  538.           JBLOCKIMAGE bsmooth[3], int * whichb, long cur_mcu_row)
  539. /* Get an MCU row of coefficients, applying cross-block smoothing. */
  540. /* The output row is placed in coeff_data.  bsmooth and whichb hold */
  541. /* working state, and cur_row is needed to check for image top/bottom. */
  542. /* This routine just takes care of the buffering logic. */
  543. {
  544.   int prev, cur, next;
  545.   
  546.   /* Special case for top of image: need to pre-fetch a row & init whichb */
  547.   if (cur_mcu_row == 0) {
  548.     (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[0]);
  549.     if (cinfo->MCU_rows_in_scan > 1) {
  550.       (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[1]);
  551.       smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], bsmooth[1],
  552.              coeff_data);
  553.     } else {
  554.       smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], (JBLOCKIMAGE) NULL,
  555.              coeff_data);
  556.     }
  557.     *whichb = 1;        /* points to next bsmooth[] element to use */
  558.     return;
  559.   }
  560.   
  561.   cur = *whichb;        /* set up references */
  562.   prev = (cur == 0 ? 2 : cur - 1);
  563.   next = (cur == 2 ? 0 : cur + 1);
  564.   *whichb = next;        /* advance whichb for next time */
  565.   
  566.   /* Special case for bottom of image: don't read another row */
  567.   if (cur_mcu_row >= cinfo->MCU_rows_in_scan - 1) {
  568.     smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], (JBLOCKIMAGE) NULL,
  569.            coeff_data);
  570.     return;
  571.   }
  572.   
  573.   /* Normal case: read ahead a new row, smooth the one I got before */
  574.   (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[next]);
  575.   smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], bsmooth[next],
  576.          coeff_data);
  577. }
  578.  
  579.  
  580. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  581.  
  582.  
  583.  
  584. /*
  585.  * Decompression pipeline controller used for single-scan files
  586.  * without 2-pass color quantization.
  587.  */
  588.  
  589. METHODDEF void
  590. single_dcontroller (decompress_info_ptr cinfo)
  591. {
  592.   long fullsize_width;        /* # of samples per row in full-size buffers */
  593.   long cur_mcu_row;        /* counts # of MCU rows processed */
  594.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  595.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  596.   /* Work buffer for dequantized coefficients (IDCT input) */
  597.   JBLOCKIMAGE coeff_data;
  598.   /* Work buffer for cross-block smoothing input */
  599. #ifdef BLOCK_SMOOTHING_SUPPORTED
  600.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  601.   int whichb;
  602. #endif
  603.   /* Work buffer for subsampled image data (see comments at head of file) */
  604.   JSAMPIMAGE subsampled_data[2];
  605.   /* Work buffer for desubsampled data */
  606.   JSAMPIMAGE fullsize_data;
  607.   /* Work buffer for color conversion output (full size) */
  608.   JSAMPIMAGE color_data;
  609.   int whichss, ri;
  610.   short i;
  611.  
  612.   /* Initialize for 1-pass color quantization, if needed */
  613.   if (cinfo->quantize_colors)
  614.     (*cinfo->methods->color_quant_init) (cinfo);
  615.  
  616.   /* Prepare for single scan containing all components */
  617.   if (cinfo->comps_in_scan == 1) {
  618.     noninterleaved_scan_setup(cinfo);
  619.     /* Need to read Vk MCU rows to obtain Vk block rows */
  620.     mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  621.   } else {
  622.     interleaved_scan_setup(cinfo);
  623.     /* in an interleaved scan, one MCU row provides Vk block rows */
  624.     mcu_rows_per_loop = 1;
  625.   }
  626.  
  627.   /* Compute dimensions of full-size pixel buffers */
  628.   /* Note these are the same whether interleaved or not. */
  629.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  630.   fullsize_width = jround_up(cinfo->image_width,
  631.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  632.  
  633.   /* Allocate working memory: */
  634.   /* coeff_data holds a single MCU row of coefficient blocks */
  635.   coeff_data = alloc_MCU_row(cinfo);
  636.   /* if doing cross-block smoothing, need extra space for its input */
  637. #ifdef BLOCK_SMOOTHING_SUPPORTED
  638.   if (cinfo->do_block_smoothing) {
  639.     bsmooth[0] = alloc_MCU_row(cinfo);
  640.     bsmooth[1] = alloc_MCU_row(cinfo);
  641.     bsmooth[2] = alloc_MCU_row(cinfo);
  642.   }
  643. #endif
  644.   /* subsampled_data is sample data before unsubsampling */
  645.   alloc_sampling_buffer(cinfo, subsampled_data);
  646.   /* fullsize_data is sample data after unsubsampling */
  647.   fullsize_data = alloc_sampimage(cinfo, (int) cinfo->num_components,
  648.                   (long) rows_in_mem, fullsize_width);
  649.   /* color_data is the result of the colorspace conversion step */
  650.   color_data = alloc_sampimage(cinfo, (int) cinfo->color_out_comps,
  651.                    (long) rows_in_mem, fullsize_width);
  652.   /* if quantizing colors, also need a one-component output area for that. */
  653.   if (cinfo->quantize_colors)
  654.     quantize_out = (*cinfo->emethods->alloc_small_sarray)
  655.                 (fullsize_width, (long) rows_in_mem);
  656.  
  657.   /* Tell the memory manager to instantiate big arrays.
  658.    * We don't need any big arrays in this controller,
  659.    * but some other module (like the output file writer) may need one.
  660.    */
  661.   (*cinfo->emethods->alloc_big_arrays)
  662.     ((long) 0,                /* no more small sarrays */
  663.      (long) 0,                /* no more small barrays */
  664.      (long) 0);                /* no more "medium" objects */
  665.      /* NB: quantizer must get any such objects at color_quant_init time */
  666.  
  667.   /* Initialize to read scan data */
  668.  
  669.   (*cinfo->methods->entropy_decoder_init) (cinfo);
  670.   (*cinfo->methods->unsubsample_init) (cinfo);
  671.   (*cinfo->methods->disassemble_init) (cinfo);
  672.  
  673.   /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  674.  
  675.   pixel_rows_output = 0;
  676.   whichss = 1;            /* arrange to start with subsampled_data[0] */
  677.  
  678.   for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  679.        cur_mcu_row += mcu_rows_per_loop) {
  680.     whichss ^= 1;        /* switch to other subsample buffer */
  681.  
  682.     /* Obtain v_samp_factor block rows of each component in the scan. */
  683.     /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  684.     /* In the noninterleaved case there might be fewer than v_samp_factor */
  685.     /* block rows remaining; if so, pad with copies of the last pixel row */
  686.     /* so that unsubsampling doesn't have to treat it as a special case. */
  687.  
  688.     for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  689.       if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  690.     /* OK to actually read an MCU row. */
  691. #ifdef BLOCK_SMOOTHING_SUPPORTED
  692.     if (cinfo->do_block_smoothing)
  693.       get_smoothed_row(cinfo, coeff_data,
  694.                bsmooth, &whichb, cur_mcu_row + ri);
  695.     else
  696. #endif
  697.       (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  698.       
  699.     reverse_DCT(cinfo, coeff_data, subsampled_data[whichss],
  700.             ri * DCTSIZE);
  701.       } else {
  702.     /* Need to pad out with copies of the last subsampled row. */
  703.     /* This can only happen if there is just one component. */
  704.     duplicate_row(subsampled_data[whichss][0],
  705.               cinfo->cur_comp_info[0]->subsampled_width,
  706.               ri * DCTSIZE - 1, DCTSIZE);
  707.       }
  708.     }
  709.  
  710.     /* Unsubsample the data */
  711.     /* First time through is a special case */
  712.  
  713.     if (cur_mcu_row) {
  714.       /* Expand last row group of previous set */
  715.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  716.          (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  717.          (short) (DCTSIZE-1));
  718.       /* and dump the previous set's expanded data */
  719.       emit_1pass (cinfo, rows_in_mem, fullsize_data, color_data);
  720.       pixel_rows_output += rows_in_mem;
  721.       /* Expand first row group of this set */
  722.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  723.          (short) (DCTSIZE+1), (short) 0, (short) 1,
  724.          (short) 0);
  725.     } else {
  726.       /* Expand first row group with dummy above-context */
  727.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  728.          (short) (-1), (short) 0, (short) 1,
  729.          (short) 0);
  730.     }
  731.     /* Expand second through next-to-last row groups of this set */
  732.     for (i = 1; i <= DCTSIZE-2; i++) {
  733.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  734.          (short) (i-1), (short) i, (short) (i+1),
  735.          (short) i);
  736.     }
  737.   } /* end of outer loop */
  738.  
  739.   /* Expand the last row group with dummy below-context */
  740.   /* Note whichss points to last buffer side used */
  741.   expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  742.      (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  743.      (short) (DCTSIZE-1));
  744.   /* and dump the remaining data (may be less than full height) */
  745.   emit_1pass (cinfo, (int) (cinfo->image_height - pixel_rows_output),
  746.           fullsize_data, color_data);
  747.  
  748.   /* Clean up after the scan */
  749.   (*cinfo->methods->disassemble_term) (cinfo);
  750.   (*cinfo->methods->unsubsample_term) (cinfo);
  751.   (*cinfo->methods->entropy_decoder_term) (cinfo);
  752.   (*cinfo->methods->read_scan_trailer) (cinfo);
  753.  
  754.   /* Verify that we've seen the whole input file */
  755.   if ((*cinfo->methods->read_scan_header) (cinfo))
  756.     ERREXIT(cinfo->emethods, "Didn't expect more than one scan");
  757.  
  758.   /* Release working memory */
  759.   free_MCU_row(cinfo, coeff_data);
  760. #ifdef BLOCK_SMOOTHING_SUPPORTED
  761.   if (cinfo->do_block_smoothing) {
  762.     free_MCU_row(cinfo, bsmooth[0]);
  763.     free_MCU_row(cinfo, bsmooth[1]);
  764.     free_MCU_row(cinfo, bsmooth[2]);
  765.   }
  766. #endif
  767.   free_sampling_buffer(cinfo, subsampled_data);
  768.   free_sampimage(cinfo, fullsize_data, (int) cinfo->num_components,
  769.          (long) rows_in_mem);
  770.   free_sampimage(cinfo, color_data, (int) cinfo->color_out_comps,
  771.          (long) rows_in_mem);
  772.   if (cinfo->quantize_colors)
  773.     (*cinfo->emethods->free_small_sarray)
  774.         (quantize_out, (long) rows_in_mem);
  775.  
  776.   /* Close up shop */
  777.   if (cinfo->quantize_colors)
  778.     (*cinfo->methods->color_quant_term) (cinfo);
  779. }
  780.  
  781.  
  782. /*
  783.  * Decompression pipeline controller used for single-scan files
  784.  * with 2-pass color quantization.
  785.  */
  786.  
  787. #ifdef QUANT_2PASS_SUPPORTED
  788.  
  789. METHODDEF void
  790. single_2quant_dcontroller (decompress_info_ptr cinfo)
  791. {
  792.   long fullsize_width;        /* # of samples per row in full-size buffers */
  793.   long cur_mcu_row;        /* counts # of MCU rows processed */
  794.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  795.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  796.   /* Work buffer for dequantized coefficients (IDCT input) */
  797.   JBLOCKIMAGE coeff_data;
  798.   /* Work buffer for cross-block smoothing input */
  799. #ifdef BLOCK_SMOOTHING_SUPPORTED
  800.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  801.   int whichb;
  802. #endif
  803.   /* Work buffer for subsampled image data (see comments at head of file) */
  804.   JSAMPIMAGE subsampled_data[2];
  805.   /* Work buffer for desubsampled data */
  806.   JSAMPIMAGE fullsize_data;
  807.   int whichss, ri;
  808.   short ci, i;
  809.  
  810.   /* Initialize for 2-pass color quantization */
  811.   (*cinfo->methods->color_quant_init) (cinfo);
  812.  
  813.   /* Prepare for single scan containing all components */
  814.   if (cinfo->comps_in_scan == 1) {
  815.     noninterleaved_scan_setup(cinfo);
  816.     /* Need to read Vk MCU rows to obtain Vk block rows */
  817.     mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  818.   } else {
  819.     interleaved_scan_setup(cinfo);
  820.     /* in an interleaved scan, one MCU row provides Vk block rows */
  821.     mcu_rows_per_loop = 1;
  822.   }
  823.  
  824.   /* Compute dimensions of full-size pixel buffers */
  825.   /* Note these are the same whether interleaved or not. */
  826.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  827.   fullsize_width = jround_up(cinfo->image_width,
  828.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  829.  
  830.   /* Allocate working memory: */
  831.   /* coeff_data holds a single MCU row of coefficient blocks */
  832.   coeff_data = alloc_MCU_row(cinfo);
  833.   /* if doing cross-block smoothing, need extra space for its input */
  834. #ifdef BLOCK_SMOOTHING_SUPPORTED
  835.   if (cinfo->do_block_smoothing) {
  836.     bsmooth[0] = alloc_MCU_row(cinfo);
  837.     bsmooth[1] = alloc_MCU_row(cinfo);
  838.     bsmooth[2] = alloc_MCU_row(cinfo);
  839.   }
  840. #endif
  841.   /* subsampled_data is sample data before unsubsampling */
  842.   alloc_sampling_buffer(cinfo, subsampled_data);
  843.   /* fullsize_data is sample data after unsubsampling */
  844.   fullsize_data = alloc_sampimage(cinfo, (int) cinfo->num_components,
  845.                   (long) rows_in_mem, fullsize_width);
  846.   /* Also need a one-component output area for color quantizer. */
  847.   quantize_out = (*cinfo->emethods->alloc_small_sarray)
  848.                 (fullsize_width, (long) rows_in_mem);
  849.  
  850.   /* Get a big image for quantizer input: desubsampled, color-converted data */
  851.   fullsize_cnvt_image = (big_sarray_ptr *) (*cinfo->emethods->alloc_small)
  852.             (cinfo->num_components * SIZEOF(big_sarray_ptr));
  853.   for (ci = 0; ci < cinfo->num_components; ci++) {
  854.     fullsize_cnvt_image[ci] = (*cinfo->emethods->request_big_sarray)
  855.             (fullsize_width,
  856.              jround_up(cinfo->image_height, (long) rows_in_mem),
  857.              (long) rows_in_mem);
  858.   }
  859.   /* Also get an area for pointers to currently accessible chunks */
  860.   fullsize_cnvt_ptrs = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  861.                 (cinfo->num_components * SIZEOF(JSAMPARRAY));
  862.  
  863.   /* Tell the memory manager to instantiate big arrays */
  864.   (*cinfo->emethods->alloc_big_arrays)
  865.     ((long) 0,                /* no more small sarrays */
  866.      (long) 0,                /* no more small barrays */
  867.      (long) 0);                /* no more "medium" objects */
  868.      /* NB: quantizer must get any such objects at color_quant_init time */
  869.  
  870.   /* Initialize to read scan data */
  871.  
  872.   (*cinfo->methods->entropy_decoder_init) (cinfo);
  873.   (*cinfo->methods->unsubsample_init) (cinfo);
  874.   (*cinfo->methods->disassemble_init) (cinfo);
  875.  
  876.   /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  877.  
  878.   pixel_rows_output = 0;
  879.   whichss = 1;            /* arrange to start with subsampled_data[0] */
  880.  
  881.   for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  882.        cur_mcu_row += mcu_rows_per_loop) {
  883.     whichss ^= 1;        /* switch to other subsample buffer */
  884.  
  885.     /* Obtain v_samp_factor block rows of each component in the scan. */
  886.     /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  887.     /* In the noninterleaved case there might be fewer than v_samp_factor */
  888.     /* block rows remaining; if so, pad with copies of the last pixel row */
  889.     /* so that unsubsampling doesn't have to treat it as a special case. */
  890.  
  891.     for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  892.       if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  893.     /* OK to actually read an MCU row. */
  894. #ifdef BLOCK_SMOOTHING_SUPPORTED
  895.     if (cinfo->do_block_smoothing)
  896.       get_smoothed_row(cinfo, coeff_data,
  897.                bsmooth, &whichb, cur_mcu_row + ri);
  898.     else
  899. #endif
  900.       (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  901.       
  902.     reverse_DCT(cinfo, coeff_data, subsampled_data[whichss],
  903.             ri * DCTSIZE);
  904.       } else {
  905.     /* Need to pad out with copies of the last subsampled row. */
  906.     /* This can only happen if there is just one component. */
  907.     duplicate_row(subsampled_data[whichss][0],
  908.               cinfo->cur_comp_info[0]->subsampled_width,
  909.               ri * DCTSIZE - 1, DCTSIZE);
  910.       }
  911.     }
  912.  
  913.     /* Unsubsample the data */
  914.     /* First time through is a special case */
  915.  
  916.     if (cur_mcu_row) {
  917.       /* Expand last row group of previous set */
  918.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  919.          (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  920.          (short) (DCTSIZE-1));
  921.       /* and dump the previous set's expanded data */
  922.       emit_2pass (cinfo, pixel_rows_output, rows_in_mem, fullsize_data);
  923.       pixel_rows_output += rows_in_mem;
  924.       /* Expand first row group of this set */
  925.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  926.          (short) (DCTSIZE+1), (short) 0, (short) 1,
  927.          (short) 0);
  928.     } else {
  929.       /* Expand first row group with dummy above-context */
  930.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  931.          (short) (-1), (short) 0, (short) 1,
  932.          (short) 0);
  933.     }
  934.     /* Expand second through next-to-last row groups of this set */
  935.     for (i = 1; i <= DCTSIZE-2; i++) {
  936.       expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  937.          (short) (i-1), (short) i, (short) (i+1),
  938.          (short) i);
  939.     }
  940.   } /* end of outer loop */
  941.  
  942.   /* Expand the last row group with dummy below-context */
  943.   /* Note whichss points to last buffer side used */
  944.   expand(cinfo, subsampled_data[whichss], fullsize_data, fullsize_width,
  945.      (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  946.      (short) (DCTSIZE-1));
  947.   /* and dump the remaining data (may be less than full height) */
  948.   emit_2pass (cinfo, pixel_rows_output,
  949.           (int) (cinfo->image_height - pixel_rows_output),
  950.           fullsize_data);
  951.  
  952.   /* Clean up after the scan */
  953.   (*cinfo->methods->disassemble_term) (cinfo);
  954.   (*cinfo->methods->unsubsample_term) (cinfo);
  955.   (*cinfo->methods->entropy_decoder_term) (cinfo);
  956.   (*cinfo->methods->read_scan_trailer) (cinfo);
  957.  
  958.   /* Verify that we've seen the whole input file */
  959.   if ((*cinfo->methods->read_scan_header) (cinfo))
  960.     ERREXIT(cinfo->emethods, "Didn't expect more than one scan");
  961.  
  962.   /* Now that we've collected the data, let the color quantizer do its thing */
  963.   (*cinfo->methods->color_quant_doit) (cinfo, scan_big_image);
  964.  
  965.   /* Release working memory */
  966.   free_MCU_row(cinfo, coeff_data);
  967. #ifdef BLOCK_SMOOTHING_SUPPORTED
  968.   if (cinfo->do_block_smoothing) {
  969.     free_MCU_row(cinfo, bsmooth[0]);
  970.     free_MCU_row(cinfo, bsmooth[1]);
  971.     free_MCU_row(cinfo, bsmooth[2]);
  972.   }
  973. #endif
  974.   free_sampling_buffer(cinfo, subsampled_data);
  975.   free_sampimage(cinfo, fullsize_data, (int) cinfo->num_components,
  976.          (long) rows_in_mem);
  977.   (*cinfo->emethods->free_small_sarray)
  978.         (quantize_out, (long) rows_in_mem);
  979.   for (ci = 0; ci < cinfo->num_components; ci++) {
  980.     (*cinfo->emethods->free_big_sarray) (fullsize_cnvt_image[ci]);
  981.   }
  982.   (*cinfo->emethods->free_small) ((void *) fullsize_cnvt_image);
  983.   (*cinfo->emethods->free_small) ((void *) fullsize_cnvt_ptrs);
  984.  
  985.   /* Close up shop */
  986.   (*cinfo->methods->color_quant_term) (cinfo);
  987. }
  988.  
  989. #endif /* QUANT_2PASS_SUPPORTED */
  990.  
  991.  
  992. /*
  993.  * Decompression pipeline controller used for multiple-scan files
  994.  * without 2-pass color quantization.
  995.  *
  996.  * The current implementation places the "big" buffer at the stage of
  997.  * desubsampled data.  Buffering subsampled data instead would reduce the
  998.  * size of temp files (by about a factor of 2 in typical cases).  However,
  999.  * the unsubsampling logic is dependent on the assumption that unsubsampling
  1000.  * occurs during a scan, so it's much easier to do the enlargement as the
  1001.  * JPEG file is read.  This also simplifies life for the memory manager,
  1002.  * which would otherwise have to deal with overlapping access_big_sarray()
  1003.  * requests.
  1004.  *
  1005.  * At present it appears that most JPEG files will be single-scan, so
  1006.  * it doesn't seem worthwhile to try to make this implementation smarter.
  1007.  */
  1008.  
  1009. #ifdef MULTISCAN_FILES_SUPPORTED
  1010.  
  1011. METHODDEF void
  1012. multi_dcontroller (decompress_info_ptr cinfo)
  1013. {
  1014.   long fullsize_width;        /* # of samples per row in full-size buffers */
  1015.   long cur_mcu_row;        /* counts # of MCU rows processed */
  1016.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  1017.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  1018.   /* Work buffer for dequantized coefficients (IDCT input) */
  1019.   JBLOCKIMAGE coeff_data;
  1020.   /* Work buffer for cross-block smoothing input */
  1021. #ifdef BLOCK_SMOOTHING_SUPPORTED
  1022.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  1023.   int whichb;
  1024. #endif
  1025.   /* Work buffer for subsampled image data (see comments at head of file) */
  1026.   JSAMPIMAGE subsampled_data[2];
  1027.   /* Full-image buffer holding desubsampled, but not color-converted, data */
  1028.   big_sarray_ptr *fullsize_image;
  1029.   JSAMPIMAGE fullsize_ptrs;    /* workspace for access_big_sarray() results */
  1030.   /* Work buffer for color conversion output (full size) */
  1031.   JSAMPIMAGE color_data;
  1032.   int whichss, ri;
  1033.   short ci, i;
  1034.  
  1035.   /* Initialize for 1-pass color quantization, if needed */
  1036.   if (cinfo->quantize_colors)
  1037.     (*cinfo->methods->color_quant_init) (cinfo);
  1038.  
  1039.   /* Compute dimensions of full-size pixel buffers */
  1040.   /* Note these are the same whether interleaved or not. */
  1041.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  1042.   fullsize_width = jround_up(cinfo->image_width,
  1043.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  1044.  
  1045.   /* Allocate all working memory that doesn't depend on scan info */
  1046.   /* color_data is the result of the colorspace conversion step */
  1047.   color_data = alloc_sampimage(cinfo, (int) cinfo->color_out_comps,
  1048.                    (long) rows_in_mem, fullsize_width);
  1049.   /* if quantizing colors, also need a one-component output area for that. */
  1050.   if (cinfo->quantize_colors)
  1051.     quantize_out = (*cinfo->emethods->alloc_small_sarray)
  1052.                 (fullsize_width, (long) rows_in_mem);
  1053.  
  1054.   /* Get a big image: fullsize_image is sample data after unsubsampling. */
  1055.   fullsize_image = (big_sarray_ptr *) (*cinfo->emethods->alloc_small)
  1056.             (cinfo->num_components * SIZEOF(big_sarray_ptr));
  1057.   for (ci = 0; ci < cinfo->num_components; ci++) {
  1058.     fullsize_image[ci] = (*cinfo->emethods->request_big_sarray)
  1059.             (fullsize_width,
  1060.              jround_up(cinfo->image_height, (long) rows_in_mem),
  1061.              (long) rows_in_mem);
  1062.   }
  1063.   /* Also get an area for pointers to currently accessible chunks */
  1064.   fullsize_ptrs = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  1065.                 (cinfo->num_components * SIZEOF(JSAMPARRAY));
  1066.  
  1067.   /* Tell the memory manager to instantiate big arrays */
  1068.   (*cinfo->emethods->alloc_big_arrays)
  1069.      /* extra sarray space is for subsampled-data buffers: */
  1070.     ((long) (fullsize_width            /* max width in samples */
  1071.      * cinfo->max_v_samp_factor*(DCTSIZE+2)    /* max height */
  1072.      * cinfo->num_components),        /* max components per scan */
  1073.      /* extra barray space is for MCU-row buffers: */
  1074.      (long) ((fullsize_width / DCTSIZE)    /* max width in blocks */
  1075.      * cinfo->max_v_samp_factor        /* max height */
  1076.      * cinfo->num_components        /* max components per scan */
  1077.      * (cinfo->do_block_smoothing ? 4 : 1)),/* how many of these we need */
  1078.      /* no extra "medium"-object space */
  1079.      /* NB: quantizer must get any such objects at color_quant_init time */
  1080.      (long) 0);
  1081.  
  1082.  
  1083.   /* Loop over scans in file */
  1084.  
  1085.   do {
  1086.     
  1087.     /* Prepare for this scan */
  1088.     if (cinfo->comps_in_scan == 1) {
  1089.       noninterleaved_scan_setup(cinfo);
  1090.       /* Need to read Vk MCU rows to obtain Vk block rows */
  1091.       mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  1092.     } else {
  1093.       interleaved_scan_setup(cinfo);
  1094.       /* in an interleaved scan, one MCU row provides Vk block rows */
  1095.       mcu_rows_per_loop = 1;
  1096.     }
  1097.     
  1098.     /* Allocate scan-local working memory */
  1099.     /* coeff_data holds a single MCU row of coefficient blocks */
  1100.     coeff_data = alloc_MCU_row(cinfo);
  1101.     /* if doing cross-block smoothing, need extra space for its input */
  1102. #ifdef BLOCK_SMOOTHING_SUPPORTED
  1103.     if (cinfo->do_block_smoothing) {
  1104.       bsmooth[0] = alloc_MCU_row(cinfo);
  1105.       bsmooth[1] = alloc_MCU_row(cinfo);
  1106.       bsmooth[2] = alloc_MCU_row(cinfo);
  1107.     }
  1108. #endif
  1109.     /* subsampled_data is sample data before unsubsampling */
  1110.     alloc_sampling_buffer(cinfo, subsampled_data);
  1111.  
  1112.     /* line up the big buffers */
  1113.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  1114.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  1115.     (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
  1116.      (long) 0, TRUE);
  1117.     }
  1118.     
  1119.     /* Initialize to read scan data */
  1120.     
  1121.     (*cinfo->methods->entropy_decoder_init) (cinfo);
  1122.     (*cinfo->methods->unsubsample_init) (cinfo);
  1123.     (*cinfo->methods->disassemble_init) (cinfo);
  1124.     
  1125.     /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  1126.     
  1127.     pixel_rows_output = 0;
  1128.     whichss = 1;        /* arrange to start with subsampled_data[0] */
  1129.     
  1130.     for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  1131.      cur_mcu_row += mcu_rows_per_loop) {
  1132.       whichss ^= 1;        /* switch to other subsample buffer */
  1133.  
  1134.       /* Obtain v_samp_factor block rows of each component in the scan. */
  1135.       /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  1136.       /* In the noninterleaved case there might be fewer than v_samp_factor */
  1137.       /* block rows remaining; if so, pad with copies of the last pixel row */
  1138.       /* so that unsubsampling doesn't have to treat it as a special case. */
  1139.       
  1140.       for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  1141.     if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  1142.       /* OK to actually read an MCU row. */
  1143. #ifdef BLOCK_SMOOTHING_SUPPORTED
  1144.       if (cinfo->do_block_smoothing)
  1145.         get_smoothed_row(cinfo, coeff_data,
  1146.                  bsmooth, &whichb, cur_mcu_row + ri);
  1147.       else
  1148. #endif
  1149.         (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  1150.       
  1151.       reverse_DCT(cinfo, coeff_data, subsampled_data[whichss],
  1152.               ri * DCTSIZE);
  1153.     } else {
  1154.       /* Need to pad out with copies of the last subsampled row. */
  1155.       /* This can only happen if there is just one component. */
  1156.       duplicate_row(subsampled_data[whichss][0],
  1157.             cinfo->cur_comp_info[0]->subsampled_width,
  1158.             ri * DCTSIZE - 1, DCTSIZE);
  1159.     }
  1160.       }
  1161.       
  1162.       /* Unsubsample the data */
  1163.       /* First time through is a special case */
  1164.       
  1165.       if (cur_mcu_row) {
  1166.     /* Expand last row group of previous set */
  1167.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  1168.            (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  1169.            (short) (DCTSIZE-1));
  1170.     /* Realign the big buffers */
  1171.     pixel_rows_output += rows_in_mem;
  1172.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  1173.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  1174.         (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
  1175.          pixel_rows_output, TRUE);
  1176.     }
  1177.     /* Expand first row group of this set */
  1178.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  1179.            (short) (DCTSIZE+1), (short) 0, (short) 1,
  1180.            (short) 0);
  1181.       } else {
  1182.     /* Expand first row group with dummy above-context */
  1183.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  1184.            (short) (-1), (short) 0, (short) 1,
  1185.            (short) 0);
  1186.       }
  1187.       /* Expand second through next-to-last row groups of this set */
  1188.       for (i = 1; i <= DCTSIZE-2; i++) {
  1189.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  1190.            (short) (i-1), (short) i, (short) (i+1),
  1191.            (short) i);
  1192.       }
  1193.     } /* end of outer loop */
  1194.     
  1195.     /* Expand the last row group with dummy below-context */
  1196.     /* Note whichss points to last buffer side used */
  1197.     expand(cinfo, subsampled_data[whichss], fullsize_ptrs, fullsize_width,
  1198.        (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  1199.        (short) (DCTSIZE-1));
  1200.     
  1201.     /* Clean up after the scan */
  1202.     (*cinfo->methods->disassemble_term) (cinfo);
  1203.     (*cinfo->methods->unsubsample_term) (cinfo);
  1204.     (*cinfo->methods->entropy_decoder_term) (cinfo);
  1205.     (*cinfo->methods->read_scan_trailer) (cinfo);
  1206.  
  1207.     /* Release scan-local working memory */
  1208.     free_MCU_row(cinfo, coeff_data);
  1209. #ifdef BLOCK_SMOOTHING_SUPPORTED
  1210.     if (cinfo->do_block_smoothing) {
  1211.       free_MCU_row(cinfo, bsmooth[0]);
  1212.       free_MCU_row(cinfo, bsmooth[1]);
  1213.       free_MCU_row(cinfo, bsmooth[2]);
  1214.     }
  1215. #endif
  1216.     free_sampling_buffer(cinfo, subsampled_data);
  1217.     
  1218.     /* Repeat if there is another scan */
  1219.   } while ((*cinfo->methods->read_scan_header) (cinfo));
  1220.  
  1221.   /* Now that we've collected all the data, color convert & output it. */
  1222.  
  1223.   for (pixel_rows_output = 0; pixel_rows_output < cinfo->image_height;
  1224.        pixel_rows_output += rows_in_mem) {
  1225.  
  1226.     /* realign the big buffers */
  1227.     for (ci = 0; ci < cinfo->num_components; ci++) {
  1228.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  1229.     (fullsize_image[ci], pixel_rows_output, FALSE);
  1230.     }
  1231.  
  1232.     emit_1pass (cinfo,
  1233.         (int) MIN((long) rows_in_mem,
  1234.               cinfo->image_height - pixel_rows_output),
  1235.         fullsize_ptrs, color_data);
  1236.   }
  1237.  
  1238.   /* Release working memory */
  1239.   free_sampimage(cinfo, color_data, (int) cinfo->color_out_comps,
  1240.          (long) rows_in_mem);
  1241.   if (cinfo->quantize_colors)
  1242.     (*cinfo->emethods->free_small_sarray)
  1243.         (quantize_out, (long) rows_in_mem);
  1244.   for (ci = 0; ci < cinfo->num_components; ci++) {
  1245.     (*cinfo->emethods->free_big_sarray) (fullsize_image[ci]);
  1246.   }
  1247.   (*cinfo->emethods->free_small) ((void *) fullsize_image);
  1248.   (*cinfo->emethods->free_small) ((void *) fullsize_ptrs);
  1249.  
  1250.   /* Close up shop */
  1251.   if (cinfo->quantize_colors)
  1252.     (*cinfo->methods->color_quant_term) (cinfo);
  1253. }
  1254.  
  1255. #endif /* MULTISCAN_FILES_SUPPORTED */
  1256.  
  1257.  
  1258. /*
  1259.  * Decompression pipeline controller used for multiple-scan files
  1260.  * with 2-pass color quantization.
  1261.  */
  1262.  
  1263. #ifdef MULTISCAN_FILES_SUPPORTED
  1264. #ifdef QUANT_2PASS_SUPPORTED
  1265.  
  1266. METHODDEF void
  1267. multi_2quant_dcontroller (decompress_info_ptr cinfo)
  1268. {
  1269.   ERREXIT(cinfo->emethods, "Not implemented yet");
  1270. }
  1271.  
  1272. #endif /* QUANT_2PASS_SUPPORTED */
  1273. #endif /* MULTISCAN_FILES_SUPPORTED */
  1274.  
  1275.  
  1276. /*
  1277.  * The method selection routine for decompression pipeline controllers.
  1278.  * Note that at this point we've already read the JPEG header and first SOS,
  1279.  * so we can tell whether the input is one scan or not.
  1280.  */
  1281.  
  1282. GLOBAL void
  1283. jseldpipeline (decompress_info_ptr cinfo)
  1284. {
  1285.   /* simplify subsequent tests on color quantization */
  1286.   if (! cinfo->quantize_colors)
  1287.     cinfo->two_pass_quantize = FALSE;
  1288.   
  1289.   if (cinfo->comps_in_scan == cinfo->num_components) {
  1290.     /* It's a single-scan file */
  1291. #ifdef QUANT_2PASS_SUPPORTED
  1292.     if (cinfo->two_pass_quantize)
  1293.       cinfo->methods->d_pipeline_controller = single_2quant_dcontroller;
  1294.     else
  1295. #endif
  1296.       cinfo->methods->d_pipeline_controller = single_dcontroller;
  1297.   } else {
  1298.     /* It's a multiple-scan file */
  1299. #ifdef MULTISCAN_FILES_SUPPORTED
  1300. #ifdef QUANT_2PASS_SUPPORTED
  1301.     if (cinfo->two_pass_quantize)
  1302.       cinfo->methods->d_pipeline_controller = multi_2quant_dcontroller;
  1303.     else
  1304. #endif
  1305.       cinfo->methods->d_pipeline_controller = multi_dcontroller;
  1306. #else
  1307.     ERREXIT(cinfo->emethods, "Multiple-scan support was not compiled");
  1308. #endif
  1309.   }
  1310. }
  1311.